<?php
//首页显示
namespace newMobile\Controller;
use Think\Controller;
class CartController extends CommonController {
    public function add(){
    	$goods_id=I('post.goods_id');
    	$shop=M('shop');
    	$shop_info=$shop->find($goods_id);
    	if($shop_info['shop_sku']){
    		$shop_info['shop_sku']=json_decode($shop_info['shop_sku'], true);
    	}  
    	$this->ajax_r(0,$shop_info,'sku属性');
	}
	public function add_cart(){
		$data=I('post.');
		$shop_sku=M('shop_sku');
		$shop=M('shop');
		$buygood=I('post.buygood',0);
		$shop_info=$shop->find($data['id']);
		if(isset($data['yanse'])||isset($data['guige'])){
			//有属性
			$map['shop_id']=$data['id'];
			if(isset($data['yanse'])){
				$map['sku_style']=$data['yanse'];
			}
			if(isset($data['guige'])){
				$map['sku_spec']=$data['guige'];
			}
			$sku_info=$shop_sku->where($map)->find();
			
		}
		//整理添加数据
		$new_cart['shop_id']=$data['id'];
		$new_cart['store_id']=$shop_info['store_id'];
		$new_cart['num']=$data['num'];
		$new_cart['select']=1;
		//$new_cart['recommend_id']=$data['rec_id'];
		$new_cart['tuanselect']=$data['type']?$data['type']:0;
		$new_cart['ctuan_id']=$data['ctuan_id']?$data['ctuan_id']:0;
		if($sku_info){
			$new_cart['sku_id']=$sku_info['sku_id'];
		}else{
			$new_cart['sku_id']=0;
		}
		//判断用户是否登录
		if(session('user_id')){
			$uid=session('user_id');
		}else{
			$uid=0;
		}
		
		if($uid==0){
			//未登录写入cookie
			$cart_array = cookie('cart_array');
			if(!empty($cart_array)){
                $cart_array = json_decode($cart_array,true);
                $tmp_array = array();
                foreach($cart_array as $k=>$v){
                    $tmp_array[] = $v['cart_id'];
                }
                $cart_id = max($tmp_array) + 1;
                $is_have = true;
                foreach($cart_array as $k=>$v){                      
                   if($v["shop_id"] ==$new_cart['shop_id'] && $v["sku_id"] == $new_cart['sku_id']){
                       $is_have = false;
                       $cart_array[$k]["num"] = $new_cart["num"] + $v["num"];
                   }
                }
                if($is_have){
                    $data["cart_id"] = $cart_id;
                    $cart_array[] = $new_cart;
                }
            }else{
                $data["cart_id"] = 1;
                $cart_array[] = $new_cart;
            } 
            $cart_array_string = json_encode($cart_array);
			try{
                cookie('cart_array', $cart_array_string, 3600);
                $this->ajax_r(1,$new_cart,'成功加入购物车！');
            }catch(\Exception $e){
                $this->ajax_r(0,$e,'加入购物车失败！');
            }
		}else{
			$rs=addcart($uid,$new_cart['shop_id'],$new_cart['sku_id'],$new_cart['num'],$new_cart['tuanselect'],$new_cart['ctuan_id'],$buygood);
			$cart_array = cookie('cart_array');
			
			if($rs==1){
				 $this->ajax_r(1,$rs,'成功加入购物车！');
			}elseif($rs==2){
				$this->ajax_r(0,'','不能参加自己所开的团！'); 
			}elseif($rs==3){
				$this->ajax_r(0,'','库存不足！'); 
			}elseif($rs==4){
				$this->ajax_r(0,'','该产品限购！'); 
			}elseif($rs==5){
				$this->ajax_r(0,'','不可重复参团！'); 
			}elseif($rs==6){
				$this->ajax_r(0,'','购买量不得小于起拍量！'); 
			}else{
				$this->ajax_r(0,'','加入购物车失败！');
			}
		}
	}
	public function del_cart(){
		
		$data_id=I('post.data_id');
		if(is_array($data_id)){
			foreach($data_id as $key=>$val){
				$val=explode(',',$val);
				$shop_id=$val[0];
				$sku_id=$val[1];
				if(session('user_id')){
					$uid=session('user_id');
					$cart=M('cart');
					$rs=$cart->where('buyer_id='.$uid.' and shop_id='.$shop_id.' and sku_id='.$sku_id)->delete();
				}else{
					$cart_array = cookie('cart_array');
					$cart_array = json_decode($cart_array,true);
					foreach($cart_array as $key => $v){
						if($v['sku_id']==$sku_id&&$v['shop_id']==$shop_id){
							unset($cart_array[$key]);
						}
					}
					$cart_array_string = json_encode($cart_array);
					cookie('cart_array', $cart_array_string, 3600);
				}
			}
		}else{
			$data_id=explode(',',$data_id);
			$shop_id=$data_id[0];
			$sku_id=$data_id[1];
			if(session('user_id')){
				$uid=session('user_id');
				$cart=M('cart');
				$rs=$cart->where('buyer_id='.$uid.' and shop_id='.$shop_id.' and sku_id='.$sku_id)->delete();
			}else{
				$cart_array = cookie('cart_array');
				$cart_array = json_decode($cart_array,true);
				foreach($cart_array as $key => $v){
					if($v['sku_id']==$sku_id&&$v['shop_id']==$shop_id){
						unset($cart_array[$key]);
					}
				}
				$cart_array_string = json_encode($cart_array);
				cookie('cart_array', $cart_array_string, 3600);
			}
		}
		
		 $this->ajax_r(1,'','删除成功');
		
	}
	
	public function get_cart_info(){
		$shop=M('shop');
		$shop_sku=M('shop_sku');
		//判断用户是否登录
		$count['num']=0;
		$count['price']=0;
		if(session('user_id')){
			$uid=session('user_id');
			$cart=M('cart');
			$cart_list=$cart->where('buyer_id='.$uid)->select();

			$cart_info;
			foreach($cart_list as $key => $v){
				
				$cart_info[$key]['num']=$v['num'];
				$cart_info[$key]['price']=$v['price'];
				$cart_info[$key]['shop_picture']=$v['shop_picture'];
				if($v['sku_id']!=0){
					$rs_shop_sku=$shop_sku->where('sku_id='.$v['sku_id'])->find();
					$cart_info[$key]['shop_name']=$v['shop_name'].'&nbsp;'.$rs_shop_sku['sku_spec'].'&nbsp;'.$rs_shop_sku['sku_style'];
				}else{
					$cart_info[$key]['shop_name']=$v['shop_name'];
				}
				$cart_info[$key]['url']=U('shop/index',array('id'=>$v['shop_id']));
				$cart_info[$key]['id']=$v['shop_id'].','.$v['sku_id'];
				$count['num']=$count['num']+$v['num'];
				$count['price']=$count['price']+($v['num']*$v['price']);
			}
		}else{
			$uid=0;
			$cart_info;
			$cart_array = cookie('cart_array');
			$cart_array = json_decode($cart_array,true);
			foreach($cart_array as $key => $v){
				$shop_id=$v['shop_id'];
				$store_id=$v['store_id'];
				$num=$v['num'];
				$sku_id=$v['sku_id'];
				$rs_shop=$shop->find($shop_id);
				if($sku_id!=0){
					$rs_shop_sku=$shop_sku->where('sku_id='.$sku_id)->find();
					$cart_info[$key]['shop_name']=$rs_shop['title'].'&nbsp;'.$rs_shop_sku['sku_spec'].'&nbsp;'.$rs_shop_sku['sku_style'];
					$cart_info[$key]['price']=$rs_shop_sku['sku_price'];
				}else{
					$cart_info[$key]['shop_name']=$rs_shop['title'];
					$cart_info[$key]['price']=$rs_shop['price'];
				}
				$cart_info[$key]['num']=$num;
				$cart_info[$key]['shop_picture']=$rs_shop['image'];
				$cart_info[$key]['url']=U('shop/index',array('id'=>$shop_id));
				$cart_info[$key]['id']=$shop_id.','.$sku_id;
				$count['num']=$count['num']+$num;
				$count['price']=$count['price']+($num*$cart_info[$key]['price']);
			}
		}
		if($cart_info){
			 $this->ajax_r(1,$cart_info,$count);
		}else{
			 $this->ajax_r(0,'',$count);
		}
	
	}
	public function cart_list(){
		$count['num']=0;
		$count['price']=0;
		
		if($_POST){
			$shop=M('shop');
			if(session('user_id')){
				$shop_sku=M('shop_sku');
				$uid=session('user_id');
                M('cart')->where("buyer_id=$uid and mian_send=1")->delete();
				$cart=M('cart');
				$cart_list=$cart->where('buyer_id='.$uid)->select();
				$cart_info;
				$result =   array();
				foreach($cart_list as $key => $v){
					
					$rs_shop=$shop->find($v['shop_id']);
					if($v['sku_id']!=0){
						$rs_shop_sku=$shop_sku->where('sku_id='.$v['sku_id'])->find();
						$v['sku_spec']=$rs_shop_sku['sku_spec'];
						$v['sku_style']=$rs_shop_sku['sku_style'];
						
					}
					//代言优惠
					$shop_id=$v['shop_id'];
				
                    $invoice=get_web_invoice();
					$start_time=time()-60*60*24*$invoice['web_diyong'];
					$daiyan_info=M('daiyan')->where("uid=$uid and shop_id=$shop_id and time>$start_time")->find();
                  
					if($daiyan_info){
						$v['daiyan_sale']=$rs_shop['dikouquan'];
					}else{
						$v['daiyan_sale']=0;
					}
					$v['url']=U('shop/index',array('id'=>$v['shop_id']));
					$v['id']=$v['shop_id'].','.$v['sku_id'];
					$v['oprice']=$rs_shop['oprice'];
					if($v['select']==1){
						$count['num']=$count['num']+$v['num'];
						$count['price']=$count['price']+($v['num']*$v['price'])-($v['num']*$v['daiyan_sale']);;
					}
					
					$result[$v['store_id']][] =$v;
				}	
			}else{
				$uid=0;
				$cart_info;
				$store=M('store');
				$cart_array = cookie('cart_array');
				$cart_array = json_decode($cart_array,true);
			
				$result =   array();
				$shop_sku=M('shop_sku');
				foreach($cart_array as $key => $v){
					$shop_id=$v['shop_id'];
					$store_id=$v['store_id'];
					$store_info=$store->find($store_id);
					$v['store_name']=$store_info['title'];
					$num=$v['num'];
					$sku_id=$v['sku_id'];
					$rs_shop=$shop->find($shop_id);
					if($sku_id!=0){
						$rs_shop_sku=$shop_sku->where('sku_id='.$sku_id)->find();
						$v['sku_spec']=$rs_shop_sku['sku_spec'];
						$v['sku_style']=$rs_shop_sku['sku_style'];
					}
					$v['daiyan_sale']=0;
					$v['shop_picture']=$rs_shop['image'];
					$v['shop_name']=$rs_shop['title'];
					$v['price']=$rs_shop['price'];
					$v['oprice']=$rs_shop['oprice'];
					$v['url']=U('shop/index',array('id'=>$shop_id));
					$v['id']=$shop_id.','.$sku_id;
					
					if($v['select']==1){
						
						$count['num']=$count['num']+$num;
						$count['price']=$count['price']+($v['num']*$v['price']);
					}
					
					
					
					$result[$v['store_id']][] =$v;
				}
			}	
			if($result){
				
			 	$this->ajax_r(1,$result,$count);
			}else{
				$this->ajax_r(0,'','购物车空');
			}
		}else{
			$this->display();
		}
		
	}
	
	function edit_cart(){
		if($_POST){
			$data=I('post.');
			$cart=M('cart');
			$goods_number=$data['goods_number'];
			$goods_number=explode(',',$goods_number);
			$shop_id=$goods_number[0];
			$sku_id=$goods_number[1];
			$shop_info=M('shop')->find($shop_id);
			//检测限购  与起拍量
			if($data['type']==1){
				$num=$data['num']+1;
			}
			if($data['type']==2){
				$num=$data['num']-1;
			}	
			if($shop_info['xz_num']>0){
				if($num>$shop_info['xz_num']){
					$this->ajax_r(0,'','该产品限购');
				}
			}
			if($shop_info['qp_num']>0){
				if($num<$shop_info['qp_num']){
					$this->ajax_r(0,'','购买量不能少于起拍量');
				}
			}
						
				
			if($num>0){
				if(session('user_id')){
					$rs=$cart->where('shop_id='.$shop_id.' and sku_id='.$sku_id.' and buyer_id='.session('user_id'))->setField('num',$num);
				}else{
					$cart_array = cookie('cart_array');
					$cart_array = json_decode($cart_array,true);
					foreach($cart_array as $k=>$v){
						if($v['shop_id']==$shop_id&& $v['sku_id'] ==$sku_id){
							$cart_array[$k]['num']=$num;
						}
					}
					$cart_array_string = json_encode($cart_array);
					cookie('cart_array', $cart_array_string, 3600);
					$rs=true;
				}
				if($rs){
					$this->ajax_r(1,'','购物车更新成功');
				}else{
					$this->ajax_r(0,'','购物车无更新');
				}
				
			}else{
				$this->ajax_r(0,'','购物车无更新');
			}
		}
	}
	
	function count_cart(){
		$data_id=I('post.data_id');
		$cart=M('cart');
		
		if(is_array($data_id)){
			$this->un_select();
			foreach($data_id as $key=>$val){
				$val=explode(',',$val);
				$shop_id=$val[0];
				$sku_id=$val[1];
				if(session('user_id')){
					$uid=session('user_id');
					$rs=$cart->where('buyer_id='.$uid.' and shop_id='.$shop_id.' and sku_id='.$sku_id)->setField('select',1);
				}else{
					$cart_array = cookie('cart_array');
					$cart_array = json_decode($cart_array,true);
					foreach($cart_array as $key => $v){
						if($v['sku_id']==$sku_id&&$v['shop_id']==$shop_id){
							$cart_array[$key]['select']=1;
						}
					}
					$cart_array_string = json_encode($cart_array);
					cookie('cart_array', $cart_array_string, 3600);
				}
			}
		}else{
			$this->un_select();
		}
		$this->ajax_r(1,'','购物车更新成功');
	}
	
	function store_select(){
		$data=I('post.');
		$cart=M('cart');
		$store_id=$data['store_id'];
		$checked=$data['checked'];
		if(session('user_id')){
			$uid=session('user_id');
			$rs=$cart->where('buyer_id='.$uid.' and store_id='.$store_id)->setField('select',$checked);
		}else{
			$cart_array = cookie('cart_array');
			$cart_array = json_decode($cart_array,true);
			foreach($cart_array as $key => $v){
				if($v['store_id']==$store_id){
					$cart_array[$key]['select']=$checked;
				}
			}
			$cart_array_string = json_encode($cart_array);
			cookie('cart_array', $cart_array_string, 3600);
		}
			
		
		$this->ajax_r(1,'','购物车更新成功');
	}
	
	public function un_select(){
		//全部取消选中
		$cart=M('cart');
		if(session('user_id')){
			$cart->where('buyer_id='.session('user_id'))->setField('select',0);
		}else{
			$cart_array = cookie('cart_array');
			$cart_array = json_decode($cart_array,true);
			foreach($cart_array as $key => $v){
				$cart_array[$key]['select']=0;
			}
			$cart_array_string = json_encode($cart_array);
			cookie('cart_array', $cart_array_string, 3600);
		}
	}
	
	function pay_sub(){
		$cart=M('cart');
		if(session('user_id')){
			$cart_list=$cart->field('cart_id')->where('buyer_id='.session('user_id').' and  `select`=1')->select();
			session('cart_list_id',$cart_list);
			
			$this->ajax_r('1','','商品获取成功，正在下单....');
		}else{
			$this->ajax_r('2','','未登录....');
		}
	}
}
?>
